home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Educational / NNV / Source / NeuronView.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  148 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "NeuronView.h"
  5. #import "NeuronCntrl.h"        // needed for extern variables
  6. #import <dpsclient/psops.h>    // for postscript functions
  7. #import <math.h>        // for sqrt function
  8.  
  9. // global variables found in NeuronCntrl.m
  10. extern int NUM_NODES;
  11. extern int NUM_INPUTS;
  12. extern int NUM_OUTPUTS;
  13. extern NXPoint *NeuronLoc;
  14. extern float *NeuronValues;
  15. extern float **Network;
  16.  
  17. #define defaultsize 10
  18.  
  19. @implementation NeuronView
  20.  
  21. // initialize the new NeuronView object...
  22. - initFrame:(const NXRect *)frameRect
  23. {  
  24.     [super initFrame:frameRect];        // standard init for View
  25.     [self setOpaque:YES];            // set background mode
  26.     [self notifyAncestorWhenFrameChanged:YES];    // for resizing scrollview
  27.     newRadius = defaultsize;            // init size for newRadius
  28.     return self;
  29. }
  30.  
  31. /* at the location determined in initNetworkDisplay, fill in a circle with color for the activity rate (output value) within neuron
  32. */
  33. - displayActivity
  34. {
  35.     int i,j;
  36.     float thickness;
  37.             
  38.     [self lockFocus];            // get access to screen for drawing    
  39.     // erase previous neurons and lines
  40.     PSsetgray(2./3.);    // set gray color of rect to follow
  41.     // draw a filled rectangle over view (erase anything before)
  42.     PSrectfill(0, 0, bounds.size.width, bounds.size.height);
  43.  
  44.     // draw connecting lines (thickness & color varies)...
  45.     for (j=0; j < (NUM_NODES - NUM_OUTPUTS); j++)
  46.       for (i=j+1; i < NUM_NODES; i++)
  47.       {
  48.           if ((thickness = Network[i][j]) != 0.0)
  49.       {
  50.         if (thickness > 0.0)
  51.               NXSetColor(NXConvertRGBToColor(1.0,0.0,0.0)); // red +
  52.         else
  53.               NXSetColor(NXConvertRGBToColor(0.0,0.0,1.0)); // blue - 
  54.         PSsetlinewidth(sqrt(thickness*thickness)*5); // abs of thickness
  55.         PSmoveto(NeuronLoc[j].x, NeuronLoc[j].y);
  56.         PSlineto(NeuronLoc[i].x, NeuronLoc[i].y);
  57.         PSstroke();
  58.       }     
  59.       }
  60.  
  61.     // draw circles (color shows activity level)
  62.     for (i = 0; i < NUM_NODES; i++)
  63.     {
  64.       NXSetColor(NXConvertRGBToColor(NeuronValues[i],0.0,(1-NeuronValues[i])));
  65.       PSarc(NeuronLoc[i].x, NeuronLoc[i].y,newRadius,0,360);
  66.       PSfill();
  67.       PSstroke();
  68.       // draw histogram rectangles to better identify activity pattern...
  69.       PSsetgray(NX_BLACK);    // set bars to black
  70.       PSrectfill(NeuronLoc[i].x, NeuronLoc[i].y-6,NeuronValues[i]*newRadius*2 ,12);
  71.       PSfill();
  72.       PSstroke();
  73.  
  74.     }
  75.     
  76.     [self unlockFocus];            // release access to screen
  77.     [window flushWindow];        // flush results to screen
  78.     return self;
  79. }
  80.  
  81. /* find the layers within the network matrix, determine a location within the view for each neuron (circle), draw lines to represent the network with color and thickness varying according to + & - values and the size of the value.
  82. */
  83. - initNetworkDisplay
  84. {
  85.     int NeuronLayer[NUM_NODES];        // array to store neurons in layers
  86.     NXPoint theLoc;
  87.     NXCoord viewX = bounds.size.width;    // get X & Y size of view
  88.     NXCoord viewY = bounds.size.height;
  89.     int i,oldi,j,layer,count;
  90.     float Xspacing, Yspacing, Xoffset, Yoffset;
  91.  
  92.     i=oldi=j=layer=count=0;
  93.     // find layers within network matrix.
  94.     while (i < NUM_NODES)
  95.     {
  96.       for (; i < NUM_NODES; i++)
  97.       {
  98.         for (j=oldi; j < i; j++)
  99.       if (Network[i][j] != 0)
  100.         break; //exit this for loop...
  101.     if (Network[i][j] != 0)
  102.       break; //exit this for loop...
  103.       }
  104.       NeuronLayer[layer] = i - oldi; // # of neurons in layer.
  105.       layer++;
  106.       oldi = i;
  107.     }
  108.  
  109.     // determine a location for each neuron within window (view)
  110.     Xspacing = viewX/layer;    Xoffset = Xspacing/2;
  111.     oldi =0;
  112.     for (i=0; i < layer; i++)
  113.     {
  114.       Yspacing = viewY/NeuronLayer[i]; Yoffset = Yspacing/2;
  115.       count=0;
  116.       theLoc.x = i*Xspacing + Xoffset;
  117.       for (j =oldi; j < (oldi + NeuronLayer[i]); j++)
  118.       {
  119.         theLoc.y = count*Yspacing + Yoffset;
  120.     NeuronLoc[j] = theLoc;
  121.     count++;
  122.       }
  123.       oldi = j;
  124.     }
  125.     return self;
  126. }
  127.  
  128. // responds when Neuron size slider is adjusted more than 1 pixel in size
  129. - adjustNeuronSize:sender
  130. {
  131.     newRadius = [sender floatValue];
  132.     [self displayActivity];
  133.     return self;
  134. }
  135.  
  136. // standard method to draw the view (responds everytime view needs display)
  137. - drawSelf:(const NXRect*)r :(int)c
  138. {
  139.     PSsetgray(2./3.);    // set gray color of rect to follow
  140.     // draw a filled rectangle over view (erase anything before)
  141.     PSrectfill(0, 0, bounds.size.width, bounds.size.height);
  142.     [self initNetworkDisplay];        // initNetworkdisplay and draw
  143.     [self displayActivity];        // display the neuron activity
  144.     return self;
  145. }
  146.  
  147. @end
  148.